/**
 * Created by cchen on 2015/6/2.
 */

var toolUtil = require('../common/utils/toolUtil');
var messageUtil = require('../common/utils/messageUtil');
var Users = mongoose.model('user');
var async = require('async');
var lotteryService = require('../services/lotteryService');
var moment = require('moment');


exports.getLotteryDetail = function (req, res) {
    var userId = req.params.userId;
    var lotteryId = req.params.lotteryId;
    var clickSource = req.cparams.clickSource;

    lotteryService.getLotteryDetailService(userId, lotteryId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLotteryDetail ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = result[1];
            data.communityName = result[0].communityName;
            data.isAuth = result[0].isAuth;
            data.lotteryNum = result[2];
            data.superPrize = result[3];
            data.isPrize = result[4];
            res.json(messageUtil.composeSucceededMessage(req, null, data));

            if (clickSource != null) {
                var message = {
                    userId: userId,
                    contentId: lotteryId,
                    type: 4,
                    clickSource: clickSource,
                    typeDesc: '抽奖',
                    createTime: new Date().getTime()
                };
                toolUtil.putMessageLogMQ(message);
            }
        }
    });
};

exports.generateLotteryTicket = function (req, res) {
    var userId = req.params.userId;
    var lotteryId = req.params.lotteryId;

    lotteryService.generateLotteryTicketService(userId, lotteryId, function (err, result) {
        if (err) {
            logger.error('Exception happened when generateLotteryTicket ...!' + err.message);

            var Num = '378';
            //for (var j = 0; j < 2; j++) {
            //    Num += Math.floor(Math.random() * 10);
            //}
            //Num += '5';

            res.json(messageUtil.composeSucceededMessage(req, null, {num: Num}));
            //res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                num: result[0],
                prize: result[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
            var key = 'lottery:user:count:' + moment().format('YYYYMMDD') + ':' + userId;
            redisClient.INCRBY(key, 1);
            redisClient.EXPIRE(key, 86400);
        }
    })
};

exports.getLotteryAwardList = function (req, res) {
    var userId = req.params.userId;
    var lotteryId = req.params.lotteryId;
    var page = req.params.page;

    lotteryService.getLotteryAwardListService(userId, lotteryId, page, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLotteryAwardList ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getLotteryDetailNew = function (req, res) {
    var userId = req.params.userId;
    var lotteryId = req.params.lotteryId;
    var clickSource = req.cparams.clickSource;

    lotteryService.getLotteryDetailNewService(userId, lotteryId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLotteryDetail ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = result[1];
            data.communityName = result[0].communityName;
            data.isAuth = result[0].isAuth;
            data.lotteryNum = result[2];
            data.prizeList = result[3];
            res.json(messageUtil.composeSucceededMessage(req, null, data));

            if (clickSource != null) {
                var message = {
                    userId: userId,
                    contentId: lotteryId,
                    type: 4,
                    clickSource: clickSource,
                    typeDesc: '抽奖',
                    createTime: new Date().getTime()
                };
                toolUtil.putMessageLogMQ(message);
            }
        }
    });
};

exports.userLotteryDraw = function (req, res) {
    var userId = req.params.userId;
    var lotteryId = req.params.lotteryId;

    lotteryService.userLotteryDrawService(userId, lotteryId, function (err, result) {
        if (err) {
            logger.error('Exception happened when userLotteryDraw ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message))
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result))
        }
    })
};

exports.getLuckyUsers = function (req, res) {
    var userId = req.params.userId;
    var lotteryId = req.params.lotteryId;
    var page = req.params.page;

    lotteryService.getLuckyUsersService(userId, lotteryId, page, function (err, result) {
        if (err) {
            logger.error('Exception happened when getLuckyUsers ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getMyLuckyRecord = function (req, res) {
    var userId = req.params.userId;
    var lotteryId = req.params.lotteryId;
    var page = req.params.page;

    lotteryService.getMyLuckyRecordService(userId, lotteryId, page, function (err, result) {
        if (err) {
            logger.error('Exception happened when getMyLuckyRecord...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message))
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result))
        }
    })
};